<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY nbsp "&#x00A0;">
]>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxml="urn:schemas-microsoft-com:xslt"
  xmlns:umbraco.library="urn:umbraco.library"
  xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes"
  xmlns:BlogLibrary="urn:BlogLibrary"
  xmlns:Blog="urn:Blog"
  exclude-result-prefixes="msxml umbraco.library BlogLibrary Blog Exslt.ExsltDatesAndTimes">
  <xsl:output method="html" omit-xml-declaration="yes"/>
  <xsl:param name="currentPage"/>
  <xsl:variable name="currentBlog" select="Blog:XmlCurrent(number($currentPage/@id))" />
  <xsl:key name="ym"
           match="*[(@isDoc or name()='node') and BlogLibrary:IsType('BlogPost', @id)]"
           use="umbraco.library:FormatDateTime(./postDate, 'MMMM yyyy')" />

  <xsl:template match="/">
    <ul class="archive">
      <xsl:for-each select="$currentBlog//*[(@isDoc or name()='node') and BlogLibrary:IsType('BlogPost', @id)
                        and generate-id()=generate-id(key('ym',umbraco.library:FormatDateTime(./postDate, 'MMMM yyyy')))]">
        <xsl:sort select="./postDate" order="descending" />
        <xsl:variable name="year" select="Exslt.ExsltDatesAndTimes:year(./postDate)" />
        <xsl:variable name="month" select="Exslt.ExsltDatesAndTimes:monthinyear(./postDate)" />
        <li class="month">
          <a href="{umbraco.library:NiceUrl(./../../@id)}">
            <xsl:value-of select="umbraco.library:FormatDateTime(./postDate, 'MMMM yyyy')" />
          </a>&nbsp;<span>
            (<xsl:value-of select="count($currentBlog//*[(@isDoc or name()='node') and BlogLibrary:IsType('BlogPost', @id) and
            Exslt.ExsltDatesAndTimes:year(./postDate) = $year and
            Exslt.ExsltDatesAndTimes:monthinyear(./postDate) = $month])" />)
          </span>
        </li>
      </xsl:for-each>
    </ul>
  </xsl:template>
</xsl:stylesheet>